# -*- coding: utf-8 -*-
import os
import time
from urllib.parse import urljoin

from requests_html import HTMLSession

from utils.GeneratorUtils import GeneratorUtils


class ParseManSit(object):

    def __init__(self, book_urls: [], actress_urls: []) -> None:
        super().__init__()
        self.start_urls = book_urls
        self.actress_urls = actress_urls
        self.session = HTMLSession()
        self.gen_tools = GeneratorUtils()
        self.default_encoding = 'utf-8'

    def parse_book_data(self):
        '''
        使用属性的book_urls爬取作品数据
        :return:
        '''
        pass

    def parse_actress_data(self):
        '''
        使用属性的actress_urls 爬取女演员数据
        :return:
        '''
        self.parse_actress_date(self.actress_urls)
        pass

    def parse_actress_date(self, *actress_urls) -> []:
        ret = []
        for url in actress_urls:
            tmp = {}
            res = self.session.get(url)
            res.encoding = 'utf-8'
            actress_detail = res.html.find('.book-detail-info>div', first=True)
            imgurl = actress_detail.find('img', first=True).attrs['src']
            actress_name = actress_detail.find('#book-detail', first=True).text
            tmp['name'] = actress_name
            tmp['avatarUrl'] = urljoin(url, imgurl)
            tmp['avatar'] = self.gen_tools.download_img(img_url=tmp['avatarUrl'], img_name=actress_name + '.jpg')
            tmp['count'] = actress_detail.find('.book-meta')[0].text
            tmp['startTime'] = actress_detail.find('.book-meta')[1].text
            tmp['size'] = actress_detail.find('.book-meta')[2].text
            tmp['intro'] = res.html.find('.book-summary', first=True).text
            print('parse actress info', tmp)
            plist = []
            book_list = res.html.find('.book-ol>.book-li')
            for book in book_list:
                if not book.find('img', first=True):
                    continue
                book_img = book.find('img', first=True).attrs['src']

                book_dict = {}
                book_dict['bookTitle'] = book.find('.book-title', first=True).text
                book_dict['bookDesc'] = book.find('.book-desc', first=True).text
                book_dict['duration'] = book.find('.tag-small')[0].text
                book_dict['bookType'] = book.find('.tag-small')[1].text
                book_dict['pubTime'] = book.find('.tag-small')[2].text
                book_dict['bookImgUrl'] = urljoin(url, book_img)
                book_dict['bookAvatar'] = self.gen_tools.download_img(img_url=book_dict['bookImgUrl'],
                                                                      img_name=os.path.join(tmp['name'], book_dict[
                                                                          'bookTitle'] + '.jpg'))

                print('parse book', book_dict)
                plist.append(book_dict)
                time.sleep(3)

            tmp['bookList'] = plist

            ret.append(tmp)

        return ret
